package com.woniu.controller.mallSystem;

import com.github.pagehelper.PageInfo;
import com.woniu.model.gh.common.Result;
import com.woniu.model.mallSystem.dto.CommodityQueryDto;
import com.woniu.model.mallSystem.entity.Commodity;
import com.woniu.model.mallSystem.vo.CommodityVo;
import com.woniu.model.mallSystem.vo.PageVo;
import com.woniu.service.mallSystem.ICommodityService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Lzj
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private ICommodityService commodityService;


    @PostMapping("/findAllCommodities")
    @ApiOperation("条件搜索商品")
    public Result findAllByCondition(@RequestBody Map<String,Object> condition){
        int pageNum = (int) condition.get("pageNum");
        int pageSize = (int) condition.get("pageSize");
        PageInfo<CommodityVo> pageInfo = commodityService.findAllCommodities(pageNum, pageSize,condition);
        return Result.ok(pageInfo);
    }
    @PostMapping("/addSearch")
    @ApiOperation("筛选商品")
    public Result advancedQuery(@RequestBody Map<String,Object> advanceCondition){
        int pageNum = (int) advanceCondition.get("pageNum");
        int pageSize = (int) advanceCondition.get("pageSize");
        PageInfo<CommodityVo> pageInfo = commodityService.advancedQuery(pageNum, pageSize,advanceCondition);
        return Result.ok(pageInfo);
    }


    @PostMapping("/findByCondition")
    @ApiOperation("高级筛选商品")
    public Result findByCondition(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                  @RequestBody CommodityQueryDto commodityQueryDto){
        PageVo<Commodity> conditionQuery = commodityService.conditionQuery(pageNum, pageSize, commodityQueryDto);

        return Result.ok(conditionQuery);
    }

    /**
     * 商品es全文搜索
     */
    @GetMapping("/search")
    public Result search(String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize) {
        // 调用service查询索引库
        Map<String, Object> result = commodityService.search(keyword, pageNum, pageSize);
        return Result.ok(result);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或修改商品")
    public Result saveOrUpdate(@RequestBody Commodity commodity){
        commodityService.saveOrUpdate(commodity);
        return Result.ok();
    }

    @GetMapping("/deleteById")
    @ApiOperation("删除商品")
    public Result deleteById(Integer id){
        commodityService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/delBatch")
    @ApiOperation("批量删除")
    public Result deleteBatch(@RequestBody List<String> ids){
        commodityService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 批量下架
     */
    @PostMapping("/offShelf")
    @ApiOperation("下架商品")
    public Result<Integer> updateProduct(@RequestBody List<Long> ids) {
        List<Commodity> list = commodityService.listByIds(ids);
        for (Commodity commodity : list) {
            commodity.setStatus(0);
        }
        commodityService.saveOrUpdateBatch(list);
        return Result.ok();
    }

    /**
     *
     * 上架商品
     */
    @PostMapping("/listingCommodity")
    @ApiOperation("上架商品")
    public Result listingCommodity(@RequestBody List<Long> ids) {
        List<Commodity> list = commodityService.listByIds(ids);
        for (Commodity commodity : list) {
                commodity.setStatus(1);
                commodityService.saveOrUpdateBatch(list);
        }
        //commodityService.saveOrUpdateBatch(list);
        return Result.ok();
    }

//    // 添加商品
//    @Idempotent(expireTime = 30, timeunit = TimeUnit.SECONDS)
//    @UpdateCommodityESAnnotation
//    @PostMapping("/add")
//    @ApiOperation("添加商品")
//    @ApiImplicitParams(
//            {
//                    @ApiImplicitParam(name = "js", value = "文件对象"),
//                    @ApiImplicitParam(name = "fileList", value = "图片文件列表")
//            }
//    )
//    public Result add(@RequestParam String js, @RequestParam("fileList") MultipartFile[] files){
//        CommodityAddDto commodityAddDto = JSONUtil.toBean(js, CommodityAddDto.class);
//
//        String bigPhoto = "";
//        for (MultipartFile file : files) {
//            try {
//                String fileName = file.getOriginalFilename();
//                String uuidFileName = ossUtil.generateUUIDFileName(fileName);
//                String key = "imgs/" + uuidFileName; // 设置文件在OSS中的存储路径
//                ossUtil.uploadFile(key, file.getInputStream());
//                bigPhoto += key + ";"; // 返回文件在OSS中的存储路径
//            } catch (Exception e) {
//                throw new BusinessException(ResultCode.FAIL);
//            }
//        }
//        Commodity goods = new Commodity();
//        BeanUtil.copyProperties(commodityAddDto, goods);
//        goods.setBigPhoto(bigPhoto);
//        commodityService.save(goods);
//        return Result.ok();
//    }
}
